using System;

using Sce.PlayStation.Core;
using Sce.PlayStation.HighLevel.Physics2D;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.HighLevel.GameEngine2D;
using Sce.PlayStation.HighLevel.GameEngine2D.Base;

namespace UWSGameJame
{
	public class Balloon : Object
	{	
		private float fillValue;
		private bool popped = false;
		Particles splashNode;
		ParticleSystem splash;
		
		public Balloon (String texfilename, float mass) : base(texfilename, mass)
		{
			fillValue = 0.0f;	
			popped = false;
			body.Sleep = false;
			body.SleepCount = 0;
		}
		
		public void fill (float fill)
		{
			fillValue += fill;	
			
			if (fillValue > 30.0f && !popped) {
				pop ();	
			}
		}
		
		public void makeDynamic ()
		{
			if(body.IsKinematic())
			{
				//body.SetBodyKinematic();
				body.BackToDynamic();
			}
			
			body.Position = this.getPosition ();
			body.Rotation = this.getRotation ();
		}
		
		public void makeStatic()
		{
			body.SetBodyKinematic();	
			popped = false;
			this.setPosition (body.Position);	
			this.setRotation(body.Rotation);			
		}
		
		public void activeUpdate ()
		{
			this.setPosition (body.Position);	
			this.setRotation(body.Rotation);
		}
		
		public void pop ()
		{
			this.sprite.Color = new Vector4(255, 255, 255, 0);
			float scale = 20.0f;
			
			this.makeDynamic();
			
			Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.RandGenerator rand = new Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.RandGenerator();

			splashNode = new Particles (500);
			splash = splashNode.ParticleSystem;
			splash.TextureInfo = new TextureInfo (new Texture2D ("Application/Resources/Textures/drop.png", false));

			splashNode.Schedule (( dt ) => 

			{
				splash.Emit.Transform = splashNode.GetWorldTransform ();        
				splash.Emit.TransformForVelocityEstimate = splashNode.GetWorldTransform ();
				splash.RenderTransform = Director.Instance.CurrentScene.GetWorldTransform (); // most probably identity
			}

			, -1);

			splashNode.AdHocDraw += () => 

			{
				// debug draw the emission rectangle in blue

				Director.Instance.GL.SetBlendMode (BlendMode.Additive);
				Director.Instance.DrawHelpers.SetColor (Colors.Blue * 0.3f);
				Director.Instance.DrawHelpers.DrawBounds2 (Bounds2.QuadMinus1_1.Scale (splash.Emit.PositionVar, Sce.PlayStation.HighLevel.GameEngine2D.Base.Math._00));
			};

			splash.Emit.Velocity = new Vector2( rand.NextFloatMinus1_1() * 10.0f, rand.NextFloatMinus1_1() * 10.0f );
			splash.Emit.VelocityVar = new Vector2( 0.2f, 0.3f );
			splash.Emit.ForwardMomentum = 0.2f;
			splash.Emit.AngularMomentun = 0.0f;
			splash.Emit.LifeSpan = 10.0f;
			splash.Emit.WaitTime = 0.005f;
			splash.Emit.ScaleStart = 0.3f * scale;
			splash.Emit.ScaleStartRelVar = 0.2f;
			splash.Emit.ScaleEnd = 0.6f * scale;
			splash.Emit.ScaleEndRelVar = 0.2f;
			splash.Simulation.Fade = 0.3f;
			splash.Simulation.BrownianScale = 300.0f;
			splash.Simulation.Gravity = 98.0f;
			
			this.sprite.AddChild(splashNode);	
			
			popped = true;
			fillValue = 0.0f;
			this.resetScale();
		}
		
		public bool endSplash(BalloonScene phys)
		{
			if(splash.IsFull)
			{
				this.sprite.RemoveChild(splashNode, false);
				return true;
			}
			else
				return false;
		}
		
		public bool isPopped()
		{
			return popped;	
		}
	}
}

